package com.ktkj.init.service.impl;

import com.ktkj.init.service.ThreadPoolService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.CountDownLatch;


@Slf4j
@Service
public class ThreadPoolServiceImpl implements ThreadPoolService {

    @Resource(name = "initThreadPool")
    private ThreadPoolTaskExecutor taskExecutor;


    @Async(value = "initThreadPool")
    @Override
    public void sleep(int number) {
        try {
            Thread.sleep(number);
            log.info("sleep!!!");
        } catch (Exception e) {
            log.error("sleep.error|", e);
        }
    }

    @Override
    public void testThreads() {
        //同步辅助类需要通过这个类(计数器)来控制所有的线程都执行完成;
        CountDownLatch countDownLatch = new CountDownLatch(3);
        log.info("所有线程执行前......countDownLatch.getCount()={}", countDownLatch.getCount());
        method1(countDownLatch);
        method2(countDownLatch);
        method3(countDownLatch);
        //保证所有线程都执行完毕，再执行以下的操作
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("所有线程执行完毕......countDownLatch.getCount()={}", countDownLatch.getCount());
        method4();
    }

    private void method1(CountDownLatch countDownLatch) {
        taskExecutor.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    log.info("===method1.start===");
                    Thread.sleep(3000);
                    log.info("===method1.end===");
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    countDownLatch.countDown();  //这个不管是否异常都需要数量减,否则会被堵塞无法结束
                }
            }
        });
    }

    private void method2(CountDownLatch countDownLatch) {
        taskExecutor.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    log.info("===method2.start===");
                    Thread.sleep(5000);
                    log.info("===method2.end===");
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    countDownLatch.countDown();  //这个不管是否异常都需要数量减,否则会被堵塞无法结束
                }
            }
        });
    }

    private void method3(CountDownLatch countDownLatch) {
        taskExecutor.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    log.info("===method3.start===");
                    Thread.sleep(7000);
                    log.info("===method3.end===");
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    countDownLatch.countDown();  //这个不管是否异常都需要数量减,否则会被堵塞无法结束
                }
            }
        });
    }

    private void method4() {
        log.info("===method4.handle===");
    }
}
